<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="queryParams" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="房屋编号" prop="houseId" align="center">
            <el-input v-model="queryParams.houseId" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">评论信息</h4>
    <el-table v-loading="loading" :data="commentList">
      <el-table-column label="评论编号" align="center" prop="commentId"/>
      <el-table-column label="房屋编号" align="center" prop="houseId"/>
      <el-table-column label="评论用户编号" align="center" prop="userId"/>
      <el-table-column label="发布时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleDetail(scope.row)"
            v-hasPermi="['domain:comment:query']"
          >评论详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['domain:comment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>

    <!-- 评论详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="form.house.houseId" disabled/>
        </el-form-item>
        <el-form-item label="房屋名称" prop="houseName">
          <el-input v-model="form.house.houseName" disabled/>
        </el-form-item>
        <el-form-item label="评论用户编号" prop="userId">
          <el-input v-model="form.user.userId" disabled/>
        </el-form-item>
        <el-form-item label="评论用户昵称" prop="nickName">
          <el-input v-model="form.user.nickName" disabled/>
        </el-form-item>
        <el-form-item label="评论内容" prop="commentDetail">
          <el-input v-model="form.comment.commentDetail" type="textarea" disabled/>
        </el-form-item>
        <el-form-item label="发布时间" prop="createTime">
          <el-input v-model="form.comment.createTime" disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listComment, getComment, delComment} from "@/api/system/comment";

export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 评论表格数据
      commentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      fromPath: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
      },
      // 表单参数
      form: {
        comment: {},
        house: {},
        user: {}
      },
      // 表单校验
      rules: {}
    };
  },
  //从指定页面进入当前页，保留之前的输入内容
  beforeRouteEnter(to, from, next) {
    next(vm => {
      // 通过 `vm` 访问组件实例,将值传入fromPath
      vm.fromPath = from.path
    })
  },
  created() {
    const houseId = this.$route.params && this.$route.params.houseId;
    if (houseId) {
      this.queryParams.houseId = houseId;
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }
  },
  methods: {
    /** 查询评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        comment: {},
        house: {},
        user: {}
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 评论详情操作 */
    handleDetail(row) {
      this.reset();
      const commentId = row.commentId;
      getComment(commentId).then(response => {
        this.form.comment = response.comment;
        this.form.user = response.user;
        this.form.house = response.house;
        this.open = true;
        this.title = "评论详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.commentId || this.ids;
      this.$modal.confirm('是否确认删除评论编号为"' + commentIds + '"的数据项？').then(function () {
        return delComment(commentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 关闭按钮 */
    close() {
      let obj = null;
      if (this.fromPath.indexOf("/related") > 0) {
        obj = {path: "/house/related"};
      } else {
        obj = {path: "/house/houses"};
      }
      this.$tab.closeOpenPage(obj);
    },
  }
};
</script>
